package com.st.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.st.entity.Emp;
import com.st.service.IDeptService;
import com.st.service.IEmpService;
import com.st.service.impl.DeptServiceImpl;
import com.st.service.impl.EmpServiceImpl;
import com.st.util.DateUtil;
import com.st.util.UploadUtil;

@WebServlet("/emp.do")
@MultipartConfig
public class EmpServlet extends BaseServlet {

	private IEmpService empService = new EmpServiceImpl();
	private IDeptService deptService = new DeptServiceImpl();

	public void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取参数
		String ename = request.getParameter("ename");
		String ephone = request.getParameter("ephone");
		String sDid = request.getParameter("did");

		// 转换成int类型，先判断不为空
		Integer did = null;
		if (sDid != null && sDid.length() > 0) {
			did = Integer.parseInt(sDid);
		}

		Emp emp = new Emp();
		emp.setEname(ename);
		emp.setEphone(ephone);
		emp.setDid(did);

		// 业务处理
		List emps = empService.search(emp);
		List depts = deptService.getAll();

		request.setAttribute("emps", emps);
		request.setAttribute("depts", depts);
		request.getRequestDispatcher("empList.jsp").forward(request, response);

		// 返回结果
	}

	public void remove(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 获取参数
		String strId = request.getParameter("eid");
		int eid = Integer.parseInt(strId);
		System.out.println("??????");
		int res = empService.remove(eid);
		// 业务处理
	
		System.out.println(eid);
		// 返回结果
		 response.setContentType("text/html;charset=utf-8");
		if (res > 0) {
			// 删除成功
			response.getWriter().write("<script>alert('删除成功'); window.location.href='empList.jsp';</script>");
		} else {
			// 删除失败
			response.getWriter().write("<script>alert('删除失败'); window.location.href='empList.jsp';</script>");
		}
	}

	public void upload(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		// 参数
		Part part = request.getPart("ephoto");
		// 字节输入流
		InputStream is = part.getInputStream();
		String path = request.getServletContext().getRealPath("upload");

		// 文件名，转换成字符串
		String fileName = UUID.randomUUID().toString();

		// 获取文件的后缀名,截取 . 后面的后缀
		String suffix = part.getSubmittedFileName().substring(part.getSubmittedFileName().lastIndexOf("."));

		// 组合文件名
		fileName = fileName + suffix;

		// 写入到服务器的硬盘上
		FileOutputStream fos = new FileOutputStream(path + "/" + fileName);

		byte[] buff = new byte[1024];

		while (true) {
			int length = is.read(buff);

			if (length < 0) {
				break;
			}

			fos.write(buff, 0, length);
		}
		fos.flush();
		fos.close();

		String streid=request.getParameter("eid");
		int eid=Integer.parseInt(streid);
		int res = empService.addPhoto(eid, fileName);
		if (res > 0) {
			response.sendRedirect("empList.jsp");
		} else {
			request.setAttribute("error", "上传失败！");
			request.getRequestDispatcher("uploadPhotos.jsp").forward(request, response);
		}
	}
//	public void addView(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//		// 获取部门列表
//		List depts = deptService.getAll();
//		// 跳转到添加员工页面
//		request.setAttribute("depts", depts);
//		request.getRequestDispatcher("empAdd.jsp").forward(request, response);
//	}
	public void add(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
		// 参数
		String ename = request.getParameter("ename");
		String ephone = request.getParameter("ephone");
		Integer did = Integer.parseInt(request.getParameter("did"));
		Date ebirthday = DateUtil.strToDate(request.getParameter("ebirthday"),"yyyy-MM-dd");
		String ephoto = UploadUtil.saveFile(request, "ephoto", "upload");
		// 业务
		Emp emp = new Emp();
		emp.setEname(ename);
		emp.setEphone(ephone);
		emp.setDid(did);
		emp.setEbirthday(ebirthday);
		emp.setEphoto(ephoto);
		
		int res =empService.add(emp);
		// 结果
		if (res > 0) {
			// 跳转
			response.sendRedirect("empList.jsp");
		}else {
			request.setAttribute("error", "添加失败");
			request.getRequestDispatcher("empAdd.jsp").forward(request, response);
		}
	}
}
